package com.vf.cloud.paas.app.master.api.itester.controller;

import java.util.LinkedList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.common.domain.ApiTest;
import com.vf.cloud.paas.app.master.common.vo.itester.NavAPI;
import com.vf.cloud.paas.common.util.R;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/itester")
public class ITesterController {
	
	@GetMapping(value = { "/navs" }, produces = "application/json; charset=utf-8")
	public R<List<NavAPI>> navs(@RequestParam(name = "appid", required = false, defaultValue = "") String appid) throws Exception {
		List<NavAPI> fList=new LinkedList<NavAPI>();
		NavAPI fnavAPI;
		List<ApiTest> PList=ApiTest.dao.find("select id,name from api_test where tag='' order by sort ");
		NavAPI cnavAPI;
		List<NavAPI> childs;
		List<ApiTest> CList;
		for(ApiTest f:PList) {
			fnavAPI=new NavAPI();
			fnavAPI.setId(f.getId());
			fnavAPI.setLabel(f.getName());
			childs=new LinkedList<NavAPI>();
			CList=ApiTest.dao.find("select id,name from api_test where tag=? order by sort ",f.getName());
			for(ApiTest c:CList) {
				cnavAPI=new NavAPI();
				cnavAPI.setId(c.getId());
				cnavAPI.setLabel(c.getName());
				childs.add(cnavAPI);
			}
			fnavAPI.setChilds(childs);
			fList.add(fnavAPI);
		}
		if(!StrKit.isBlank(appid)) {
			
		}
		return R.ok(fList);
	}
	
	@GetMapping(value = { "/nav" }, produces = "application/json; charset=utf-8")
	public R<ApiTest> nav(@RequestParam(name = "id", required = false, defaultValue = "") String id) throws Exception {
		
		if(StrKit.isBlank(id)) {
			return R.failed("参数必填.");
		}
		ApiTest apiTest=ApiTest.dao.findById(id);
		if(apiTest==null) {
			return R.failed("api不存在.");
		}
		return R.ok(apiTest);
	}

}
